package com.hzncc.flowable_diboot.flowable.servicetask.payment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diboot.core.holder.ThreadLocalHolder;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.ContextHolder;
import com.hzncc.flowable_diboot.contract.entity.ContractAudit;
import com.hzncc.flowable_diboot.contract.entity.ContractInfo;
import com.hzncc.flowable_diboot.contract.service.ContractAuditService;
import com.hzncc.flowable_diboot.contract.service.ContractInfoService;
import com.hzncc.flowable_diboot.enums.ContractAuditStatusEnum;
import com.hzncc.flowable_diboot.exception.BusinessException;
import com.hzncc.flowable_diboot.payment.entity.PaymentContract;
import com.hzncc.flowable_diboot.payment.entity.PaymentRegister;
import com.hzncc.flowable_diboot.payment.service.PaymentContractService;
import com.hzncc.flowable_diboot.payment.service.PaymentRegisterService;
import com.hzncc.flowable_diboot.payment.service.impl.PaymentContractServiceImpl;
import com.hzncc.flowable_diboot.payment.service.impl.PaymentRegisterServiceImpl;
import com.hzncc.flowable_diboot.payment.vo.PaymentContractDetailVO;
import com.hzncc.flowable_diboot.payment.vo.SapPaymentContractDetailVO;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.net.ContentHandler;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 新增预收回款登记
 */
@Service
public class CreatePrePaymentRegister implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) {

        PaymentContractService paymentContractService = ContextHolder.getBean(PaymentContractServiceImpl.class);
        PaymentRegisterService paymentRegisterService = ContextHolder.getBean(PaymentRegisterServiceImpl.class);

        String instanceId = execution.getProcessInstanceId();
        LambdaQueryWrapper<PaymentContract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentContract::getInstanceId,instanceId).eq(PaymentContract::isDeleted,0);
        ThreadLocalHolder.setIgnoreInterceptor();
        PaymentContract paymentContract = paymentContractService.getSingleEntity(queryWrapper);
        if (ObjectUtils.isEmpty(paymentContract)){
            throw new BusinessException("合同收款不存在");
        }
        if ("20".equals(paymentContract.getPaymentContractCategory())){
            String paymentRegisterId = paymentContract.getPaymentRegisterId();
            ThreadLocalHolder.setIgnoreInterceptor();
            PaymentRegister paymentRegister = paymentRegisterService.getEntity(paymentRegisterId);
            PaymentRegister newPrePaymentRegister = BeanUtils.cloneBean(paymentRegister);
            //查询当前客户(分收款租户)之前的预收回款登记
            LambdaQueryWrapper<PaymentRegister> paymentRegisterQuery = new LambdaQueryWrapper<>();
            paymentRegisterQuery.eq(StringUtils.isNotEmpty(paymentContract.getSapCustomer()),PaymentRegister::getSapCustomerId,paymentContract.getSapCustomer())
                    .eq(StringUtils.isNotEmpty(paymentContract.getSapCustomerCode()),PaymentRegister::getCustomerCode,paymentContract.getSapCustomerCode())
                    .eq(PaymentRegister::getCategory,"20")
                    .eq(PaymentRegister::isDeleted,0);
            ThreadLocalHolder.setIgnoreInterceptor();
            List<PaymentRegister> oldPrePaymentRegisterList = paymentRegisterService.getEntityList(paymentRegisterQuery);
            PaymentRegister oldPrePaymentRegister = oldPrePaymentRegisterList.stream().filter(item->item.getTenantId().equals(paymentContract.getTenantId())).findFirst().orElse(null);
            if (ObjectUtils.isEmpty(oldPrePaymentRegister)){
                newPrePaymentRegister.setId(null);
                newPrePaymentRegister.setCategory("20");
                newPrePaymentRegister.setPaymentAmount(paymentContract.getAdvanceAmount());
                newPrePaymentRegister.setReceivedAmount(new BigDecimal(0));
                newPrePaymentRegister.setUnclaimedAmount(paymentContract.getAdvanceAmount());
                newPrePaymentRegister.setTenantId(paymentContract.getTenantId());
                newPrePaymentRegister.setStatus("10");
                paymentRegisterService.createEntity(newPrePaymentRegister);
            }else {
                newPrePaymentRegister.setId(oldPrePaymentRegister.getId());
                newPrePaymentRegister.setCategory("20");
                newPrePaymentRegister.setPaymentAmount(oldPrePaymentRegister.getPaymentAmount().add(paymentContract.getAdvanceAmount()));
                newPrePaymentRegister.setReceivedAmount(oldPrePaymentRegister.getReceivedAmount());
                newPrePaymentRegister.setUnclaimedAmount(newPrePaymentRegister.getPaymentAmount().subtract(newPrePaymentRegister.getReceivedAmount()));
                newPrePaymentRegister.setTenantId(paymentContract.getTenantId());
                if (newPrePaymentRegister.getReceivedAmount().compareTo(BigDecimal.ZERO)>0){
                    newPrePaymentRegister.setStatus("20");
                }else {
                    newPrePaymentRegister.setStatus("10");
                }
                paymentRegisterService.updateEntity(newPrePaymentRegister);
            }
//            LambdaUpdateWrapper<PaymentContract> paymentContractUpdate = new LambdaUpdateWrapper<>();
//            paymentContractUpdate.set(PaymentContract::getPaymentRegisterId,newPrePaymentRegister.getId()).eq(PaymentContract::getId,paymentContract.getId());
//            ThreadLocalHolder.setIgnoreInterceptor();
//            paymentContractService.updateEntity(paymentContractUpdate);
        }
    }
}
